package com.yaozheng.vocationaltraining.adapter;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.bean.HundredQuestionsEncyclopediaItemBean;

import android.app.Activity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

/**
 * 百问百答设备器
 * 
 * @author Administrator
 * 
 */
public class HundredQuestionsEncyclopediaAdapter extends BaseAdapter {

	private LayoutInflater mInflater;//
	private Activity mContext;

	private List<HundredQuestionsEncyclopediaItemBean> data = null;
	private int resolved = -1;
	private int userId = -1;

	private int selectedPosition = -1;

	public HundredQuestionsEncyclopediaAdapter(Activity context,
									 List<HundredQuestionsEncyclopediaItemBean> data) {
		this.mInflater = LayoutInflater.from(context);
		this.mContext = context;
		this.data = data;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(
					R.layout.question_list_item, null);
			holder = new ViewHolder();

			holder.edit_imageview = (ImageView) convertView
					.findViewById(R.id.edit_imageview);
			holder.cate_name_textview = (TextView) convertView
					.findViewById(R.id.cate_name_textview);
			holder.title_textview = (TextView) convertView
					.findViewById(R.id.title_textview);

			holder.reply_tip_layout = (LinearLayout) convertView
					.findViewById(R.id.reply_tip_layout);
			holder.reply_num_textview = (TextView) convertView.findViewById(R.id.reply_num_textview);

			holder.praise_tip_layout = (LinearLayout) convertView.findViewById(R.id.praise_tip_layout);
			holder.praise_num_textview = (TextView) convertView.findViewById(R.id.praise_num_textview);

			holder.reward_tip_layout = (LinearLayout) convertView.findViewById(R.id.reward_tip_layout);
			holder.reward_num_textview = (TextView) convertView.findViewById(R.id.reward_num_textview);

			holder.click_tip_layout = (LinearLayout) convertView.findViewById(R.id.click_tip_layout);
			holder.click_num_textview = (TextView) convertView.findViewById(R.id.click_num_textview);

			holder.time_textview = (TextView) convertView
					.findViewById(R.id.time_textview);

			convertView.setTag(holder);//
		} else {
			holder = (ViewHolder) convertView.getTag();//
		}

		holder.cate_name_textview.setText(data.get(position).getCateName());
		holder.title_textview.setText(data.get(position).getTitle());

		if (data.get(position).getAnswer() < 1){
			holder.reply_tip_layout.setVisibility(View.GONE);
		}else{
			holder.reply_tip_layout.setVisibility(View.VISIBLE);
			holder.reply_num_textview.setText(data.get(position).getAnswer()+"");
		}

		if(data.get(position).getLike() < 1){
			holder.praise_tip_layout.setVisibility(View.GONE);
		}else{
			holder.praise_tip_layout.setVisibility(View.VISIBLE);
			holder.praise_num_textview.setText(data.get(position).getLike()+"");
		}

		if(data.get(position).getScore() < 1){
			holder.reward_tip_layout.setVisibility(View.GONE);
		}else{
			holder.reward_tip_layout.setVisibility(View.VISIBLE);
			holder.reward_num_textview.setText(data.get(position).getScore() + "");
		}

		if(data.get(position).getClick() < 1){
			holder.click_tip_layout.setVisibility(View.GONE);
		}else{
			holder.click_tip_layout.setVisibility(View.VISIBLE);
			holder.click_num_textview.setText(data.get(position).getClick()+"");
		}

		if(data.get(position).getUid() == userId){
			holder.edit_imageview.setVisibility(View.INVISIBLE);
		}else {
			holder.edit_imageview.setVisibility(View.VISIBLE);
		}
		if(data.get(position).getResolved() != 0 ){//已解决
			holder.edit_imageview.setVisibility(View.INVISIBLE);
		}else {
			holder.edit_imageview.setVisibility(View.VISIBLE);
		}

		holder.time_textview.setText(data.get(position).getCreatedTime());

		return convertView;
	}

	public void setData(List<HundredQuestionsEncyclopediaItemBean> data,int resolved,int userId) {
		this.resolved = resolved;
		this.userId = userId;
		this.data = data;
	}

	public void setselectedPosition(int selectedPosition) {
		notifyDataSetChanged();;
	}

	class ViewHolder {
		ImageView edit_imageview;
		TextView cate_name_textview;
		TextView title_textview;

		LinearLayout reply_tip_layout;
		TextView reply_num_textview;

		LinearLayout praise_tip_layout;
		TextView praise_num_textview;

		LinearLayout reward_tip_layout;
		TextView reward_num_textview;

		LinearLayout click_tip_layout;
		TextView click_num_textview;

		TextView time_textview;
	}

}
